<?php
// @formatter:off
/**
 * @file WCDirectoriesHolder.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-04 03:02:53 +0000 (Tue, 04 Jun 2013) $
 *
 * $Id: WCDirectoriesHolder.php 68 2013-06-04 03:02:53Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/includes/holders/WCDirectoriesHolder.php $
 */
// @formatter:on

require_once "{$wcPaths["includes-directory"]}/representations/WCDirectory.php";

/**
 * @class WCDirectoriesHolder
 * This class is a holder for representations of table '%directories', in other
 * words, a holder for WCDirectory objects.
 */
class WCDirectoriesHolder extends WCGHolder {
	/**
	 * @var WCComicsHolder Pointer to the singleton instance.
	 */
	protected static $_Instance;
	/**
	 * @var string Class name of the objects to be held.
	 */
	protected $_PropertiesItemClass = "WCDirectory";
	/**
	 * @var string This is the prefix of every column on the table
	 * '%directories'.
	 */
	protected $_PropertiesPrefix = "dir_";
	/**
	 * @var string This is the name of the table represented by WCDirectory
	 * objects. In this case, '%directories'.
	 */
	protected $_PropertiesTable = "directories";
	/**
	 * This method looks for directories that match, at least partially with a
	 * keyword.
	 *
	 * @param string $keywords Word to look for.
	 * @param WCGroup $group Directories must also match this group.
	 * @return WCDirectory[] Returns a list of found directories.
	 */
	public function directoriesLike($keywords, WCGroup &$group) {
		//
		// Setting the default value to be returned.
		$out = array();
		//
		// Creating a statement to look for matches.
		// @{
		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__;
		if($this->_db->has($stmtId)) {
			$stmt = $this->_db->get($stmtId);
		} else {
			$query = "select  *\n";
			$query .= "from    {$this->_dbprefix}directories\n";
			$query .= "where   dir_group = :group\n";
			$query .= " and    lower(dir_name) like :filter\n";

			$stmt = $this->_db->prepare($stmtId, $query);
		}
		// @}
		//
		// Asking for collections.
		// @{
		$params = array(
			":group" => $group->id,
			":filter" => "%".strtolower($keywords)."%"
		);
		if($stmt->execute($params)) {
			foreach($stmt->fetchAll() as $row) {
				$directory = $this->item($row["dir_id"], $group);
				$out[] = $directory;
			}
		}
		// @}
		//
		// Sorting results by their names.
		uasort($out, "WCDirectory::NameSort");
		//
		// Returning the list of found collections.
		return $out;
	}
	//
	// Public class methods.
	/**
	 * This class method allows to get the sole instance of this holder.
	 *
	 * @return WCDirectoriesHolder Returns the singleton instance of this
	 * class.
	 */
	public static function Instance() {
		//
		// Checking if there's no instance yet.
		if(!isset(self::$_Instance)) {
			$c = __CLASS__;
			//
			// Creating the sole instance.
			self::$_Instance = new $c();
		}
		//
		// Returning the class instance.
		return self::$_Instance;
	}
}
?>